package com.century.sjt.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.century.sjt.R;
import com.century.sjt.fragment.InNumberFragment;
import com.century.sjt.fragment.InPeopleFragment;
import com.century.sjt.widget.TitleBar.SjtTitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author:周玲波
 * @Description: 我的邀请
 * @date : 2016/8/16 10:06
 */
public class MyInviteNumberActivity extends BaseActivity {
    @BindView(R.id.invite_title_bar)
    SjtTitleBar title;
    @BindView(R.id.mviewpager)
    ViewPager mViewPager;
    @BindView(R.id.id_in_number)
    TextView mNumberTextView;
    @BindView(R.id.id_in_people)
    TextView mPeopleTextView;
    @BindView(R.id.id_in_tabline)
    ImageView mTabline;
    @BindView(R.id.in_tab1)
    LinearLayout mtab1;
    @BindView(R.id.in_tab2)
    LinearLayout mtab2;

    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mDatas;
    private int mScreen1_2;
    private int mCurrentPageIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sjt_my_invitenumber_activity);
        ButterKnife.bind(this);
        initTableLineWidth();
        init();
    }

    /**
     * @Description：设置滑动条的宽度为屏幕宽度的1/2
     * @author: 周玲波
     * @date: 2016/8/16 10:58
     */
    private void initTableLineWidth() {
        Display display = getWindow().getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        display.getMetrics(outMetrics);
        mScreen1_2 = outMetrics.widthPixels / 2;
        ViewGroup.LayoutParams lp = mTabline.getLayoutParams();
        lp.width = mScreen1_2;
        mTabline.setLayoutParams(lp);
    }

    /**
     * @Description:设置滑动点击切换
     * @author: 周玲波
     * @date: 2016/8/16 11:17
     */
    private void init() {
        title.setTitle(getResources().getString(R.string.s_invite));
        title.setLeftIconResource(getResources().getString(R.string.icon_return));
        title.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mDatas = new ArrayList<>();
        InNumberFragment tab01 = new InNumberFragment();
        InPeopleFragment tab02 = new InPeopleFragment();
        mDatas.add(tab01);
        mDatas.add(tab02);
        mAdapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mDatas.get(position);
            }

            @Override
            public int getCount() {
                return mDatas.size();
            }
        };
        mViewPager.setAdapter(mAdapter);
        mtab1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            clickRest(0);
            }
        });
        mtab2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickRest(1);
            }
        });
        clickRest(0);
       setViewPager();
    }
    /**
     * @Description: 实现点击重置
     * @author: 周玲波
     * @date: 2016/8/16 14:51
     */
    private void clickRest(int position){
        if(position==0){
//            mNumberTextView.setTextColor(getResources().getColor(R.color.textdefault));
//            mPeopleTextView.setTextColor(getResources().getColor(R.color.black));
        }else{
//            mNumberTextView.setTextColor(getResources().getColor(R.color.black));
//            mPeopleTextView.setTextColor(getResources().getColor(R.color.textdefault));
        }
        mViewPager.setCurrentItem(position);
    }
     /**
      * @Description:设置ViewpPager滑动
      * @author: 周玲波
      * @date: 2016/8/16 14:59
      */
    private void setViewPager(){
    mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTabline
                    .getLayoutParams();

            if (mCurrentPageIndex == 0 && position == 0)// 0->1
            {
                lp.leftMargin = (int) (positionOffset * mScreen1_2 + mCurrentPageIndex
                        * mScreen1_2);
            } else if (mCurrentPageIndex == 1 && position == 0)// 1->0
            {
                lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_2 + (positionOffset - 1)
                        * mScreen1_2);
            } else if (mCurrentPageIndex == 1 && position == 1) // 1->2
                {
                lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_2 + positionOffset
                        * mScreen1_2);
            }
            mTabline.setLayoutParams(lp);
        }

        @Override
        public void onPageSelected(int position) {
            switch(position){
                case 0:
                    clickRest(position);
                    break;
                case  1:
                    clickRest(position);
                    break;
            }
            mCurrentPageIndex=position;
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });


    }


    @Override
    protected void initView() {

    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {

    }
}
